﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.StockCountServiceRef;

    public class CCRequest : BasePage
    {
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnSave;
        protected DropDownList ddlStatus;
        protected HtmlForm F5501;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPrID;
        protected HtmlHead Head1;
        protected HtmlImage img1;
        protected HtmlImage img2;
        protected HtmlImage img3;
        protected HtmlImage img4;
        protected HtmlImage img5;
        protected HtmlImage imgOwnerID;
        protected HtmlImage imgPutawayLoc;
        protected HtmlImage imgZoneID;
        protected Label Label1;
        protected Label Label2;
        protected Label lblAreaIDEnd;
        protected Label lblAreaIDStart;
        protected Label lblLocEnd;
        protected Label lblLocLevelEnd;
        protected Label lblLocLevelStart;
        protected Label lblLocStart;
        protected Label lblMsg;
        protected Label lblOwnerEnd;
        protected Label lblOwnerStart;
        protected Label lblRequestID;
        protected Label lblSkuEnd;
        protected Label lblSkuStart;
        protected Label lblStatus;
        protected Label lblTransfer;
        protected Label lblZoneIDEnd;
        protected Label lblZoneIDStart;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAreaIDEnd;
        protected TextBox txtAreaIDStart;
        protected TextBox txtLocEnd;
        protected TextBox txtLocLevelEnd;
        protected TextBox txtLocLevelStart;
        protected TextBox txtLocStart;
        protected TextBox txtOwnerEnd;
        protected TextBox txtOwnerStart;
        protected TextBox txtRequestID;
        protected TextBox txtSkuEnd;
        protected TextBox txtSkuStart;
        protected TextBox txtZoneIDEnd;
        protected TextBox txtZoneIDStart;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5710_001");
                }
                else
                {
                    List<string> DeleteID = new List<string> {
                        this.txtRequestID.Text
                    };
                    this.lblMsg.Text = StockCountServiceClientProxy.GetInstance().DeleteCycleCountRequest(DeleteID) + base.GetGlobalMsgResource("C006");
                    this.hdnFlag.Value = "";
                    this.txtRequestID.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            if (this.hdnFrom.Value == "list")
            {
                base.Response.Redirect("CCRequestList.aspx?from=edit", false);
            }
            else
            {
                base.Response.Redirect("CCRequestQuery.aspx", false);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.hdnFlag.Value))
                {
                    this.InsertCycleCountRequest();
                }
                else
                {
                    this.UpdateCycleCountRequest();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void InsertCycleCountRequest()
        {
            try
            {
                string PrID = string.Empty;
                CycleCountRequestInfo cycleCountRequestInfo = new CycleCountRequestInfo {
                    RequestID = this.txtRequestID.Text.Trim(),
                    Status = this.ddlStatus.SelectedValue,
                    OwnerIdStart = this.txtOwnerStart.Text,
                    OwnerIdEnd = this.txtOwnerEnd.Text,
                    SkuIdStart = this.txtSkuStart.Text,
                    SkuIdEnd = this.txtSkuEnd.Text,
                    LocStart = this.txtLocStart.Text,
                    LocEnd = this.txtLocEnd.Text
                };
                if (!string.IsNullOrEmpty(this.txtLocLevelStart.Text))
                {
                    cycleCountRequestInfo.LocLevelStart = Convert.ToInt32(this.txtLocLevelStart.Text);
                }
                if (!string.IsNullOrEmpty(this.txtLocLevelEnd.Text))
                {
                    cycleCountRequestInfo.LocLevelEnd = Convert.ToInt32(this.txtLocLevelEnd.Text);
                }
                cycleCountRequestInfo.ZoneIdStart = this.txtZoneIDStart.Text;
                cycleCountRequestInfo.ZoneIdEnd = this.txtZoneIDEnd.Text;
                cycleCountRequestInfo.AreaIdStart = this.txtAreaIDStart.Text;
                cycleCountRequestInfo.AreaIdEnd = this.txtAreaIDEnd.Text;
                cycleCountRequestInfo.CreatedBy = base.UserInfo.UserID;
                PrID = StockCountServiceClientProxy.GetInstance().InsertCycleCountRequest(cycleCountRequestInfo);
                if (!string.IsNullOrEmpty(PrID))
                {
                    this.hdnPrID.Value = PrID;
                    this.lblMsg.Text = PrID + base.GetGlobalMsgResource("C004");
                    this.lblMsg.Visible = true;
                    this.txtRequestID.Text = PrID;
                    this.txtRequestID.Enabled = false;
                    this.hdnFlag.Value = "1";
                    base.DataEntityViewState = cycleCountRequestInfo;
                    this.SetPhysicalRequestInfo2Form();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "PCStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnPrID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnFlag.Value = "1";
                this.SetPhysicalRequestInfo2Form();
            }
            else
            {
                this.ddlStatus.SelectedValue = "New";
                this.ddlStatus.Enabled = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPhysicalRequestInfo2Form()
        {
            try
            {
                string PhysicalRequestID = this.hdnPrID.Value.ToString();
                CycleCountRequestInfo cycleCountRequestInfo = StockCountServiceClientProxy.GetInstance().GetCycleCountRequestInfoByID(PhysicalRequestID);
                base.DataEntityViewState = cycleCountRequestInfo;
                if (cycleCountRequestInfo != null)
                {
                    this.txtRequestID.Text = cycleCountRequestInfo.RequestID;
                    this.txtRequestID.Enabled = false;
                    this.ddlStatus.SelectedValue = cycleCountRequestInfo.Status;
                    this.ddlStatus.Enabled = false;
                    this.txtOwnerStart.Text = cycleCountRequestInfo.OwnerIdStart;
                    this.txtOwnerEnd.Text = cycleCountRequestInfo.OwnerIdEnd;
                    this.txtSkuStart.Text = cycleCountRequestInfo.SkuIdStart;
                    this.txtSkuEnd.Text = cycleCountRequestInfo.SkuIdEnd;
                    this.txtLocStart.Text = cycleCountRequestInfo.LocStart;
                    this.txtLocEnd.Text = cycleCountRequestInfo.LocEnd;
                    this.txtLocLevelStart.Text = cycleCountRequestInfo.LocLevelStart.ToString();
                    this.txtLocLevelEnd.Text = cycleCountRequestInfo.LocLevelEnd.ToString();
                    this.txtZoneIDStart.Text = cycleCountRequestInfo.ZoneIdStart;
                    this.txtZoneIDEnd.Text = cycleCountRequestInfo.ZoneIdEnd;
                    this.txtAreaIDStart.Text = cycleCountRequestInfo.AreaIdStart;
                    this.txtAreaIDEnd.Text = cycleCountRequestInfo.AreaIdEnd;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateCycleCountRequest()
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5710_001");
                }
                else
                {
                    string PrID = string.Empty;
                    CycleCountRequestInfo cycleCountRequestInfo = base.DataEntityViewState as CycleCountRequestInfo;
                    cycleCountRequestInfo.OwnerIdStart = this.txtOwnerStart.Text;
                    cycleCountRequestInfo.OwnerIdEnd = this.txtOwnerEnd.Text;
                    cycleCountRequestInfo.SkuIdStart = this.txtSkuStart.Text;
                    cycleCountRequestInfo.SkuIdEnd = this.txtSkuEnd.Text;
                    cycleCountRequestInfo.LocStart = this.txtLocStart.Text;
                    cycleCountRequestInfo.LocEnd = this.txtLocEnd.Text;
                    if (!string.IsNullOrEmpty(this.txtLocLevelStart.Text))
                    {
                        cycleCountRequestInfo.LocLevelStart = Convert.ToInt32(this.txtLocLevelStart.Text);
                    }
                    if (!string.IsNullOrEmpty(this.txtLocLevelEnd.Text))
                    {
                        cycleCountRequestInfo.LocLevelEnd = Convert.ToInt32(this.txtLocLevelEnd.Text);
                    }
                    cycleCountRequestInfo.ZoneIdStart = this.txtZoneIDStart.Text;
                    cycleCountRequestInfo.ZoneIdEnd = this.txtZoneIDEnd.Text;
                    cycleCountRequestInfo.AreaIdStart = this.txtAreaIDStart.Text;
                    cycleCountRequestInfo.AreaIdEnd = this.txtAreaIDEnd.Text;
                    cycleCountRequestInfo.UpdatedBy = base.UserInfo.UserID;
                    PrID = StockCountServiceClientProxy.GetInstance().UpdateCycleCountRequest(cycleCountRequestInfo);
                    if (!string.IsNullOrEmpty(PrID))
                    {
                        this.lblMsg.Text = PrID + base.GetGlobalMsgResource("C002");
                        this.lblMsg.Visible = true;
                        base.DataEntityViewState = cycleCountRequestInfo;
                        this.SetPhysicalRequestInfo2Form();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }
    }
}

